package com.bmob.lover.ui.fragment.user

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.theone.common.constant.BundleConstant
import com.theone.common.ext.getValue
import com.bmob.lover.data.model.enums.TYPE_REGISTER
import com.bmob.lover.databinding.FragmentRegisterBinding
import com.bmob.lover.ui.fragment.base.BaseFragment
import com.bmob.lover.viewmodel.request.RegisterRequestViewModel
import com.bmob.lover.viewmodel.state.RegisterStateViewModel
import com.theone.mvvm.ext.qmui.*

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 * @author The one
 * @date 2021-05-14 14:10
 * @describe 短信验证码验证
 * @email 625805189@qq.com
 * @remark
 */
class RegisterFragment private constructor() :
    BaseFragment<RegisterStateViewModel, FragmentRegisterBinding>() {

    companion object {
        fun newInstance(phone: String? = null): RegisterFragment {
            return RegisterFragment().apply {
                phone?.let {
                    arguments = Bundle().apply {
                        putString(BundleConstant.DATA, it)
                    }
                }
            }
        }
    }

    private val mRegisterRequestVm: RegisterRequestViewModel by viewModels()

    private val mPhone: String? by getValue(BundleConstant.DATA)

    override fun initView(root: View) {
        addLeftCloseImageBtn()
    }

    override fun initData() {
        mPhone?.let {
            getViewModel().phoneNumber.set(it)
            getViewModel().account.set(it)
        }
    }

    override fun createObserver() {
        addLoadingObserve(mRegisterRequestVm)
        mRegisterRequestVm.getResponseLiveData().observe(this) {
            mAppVm.setUserInfo(it)
            context?.showTipsDelayedDismiss("注册成功", QMUITipDialog.Builder.ICON_TYPE_SUCCESS, 1000) {
                startFragmentAndDestroyCurrent(UserInfoModifyFragment.newInstance(TYPE_REGISTER))
            }
        }
        mRegisterRequestVm.getErrorLiveData().observe(this){
            if(it.contains("username '${getViewModel().account.get()}' already taken")){
                showFailTipsDialog("用户名 ${getViewModel().account.get()} 已经存在",2000)
            }else{
                showFailTipsDialog(it)
            }
        }
    }

    override fun getBindingClick() = ProxyClick()

    inner class ProxyClick {

        fun register() {
            with(getViewModel()) {
                val code = code.get()
                val password = password.get()
                val rePassword = repassword.get()
                val account = account.get()
                when {
                    code.isEmpty() && null != mPhone -> {
                        showFailTipsDialog("验证码不能为空")
                    }
                    code.length != 6 && null != mPhone -> {
                        showFailTipsDialog("验证码为6位数的数字")
                    }
                    account.isEmpty() -> showFailTipsDialog("请填写用户名")
                    password.isEmpty() -> showFailTipsDialog("请填写密码")
                    rePassword.isEmpty() -> showFailTipsDialog("请填写确认密码")
                    password.length < 6 -> showFailTipsDialog("密码长度太短")
                    password != rePassword -> showFailTipsDialog("两次密码输入不相同")
                    else -> {
                        mRegisterRequestVm.register(account, mPhone, code, password)
                    }
                }
            }
        }

        fun registerByPhone() {
            startFragmentAndDestroyCurrent(SendVerifyCodeFragment.newInstance(TYPE_REGISTER))
        }
    }

}